﻿using UnityEngine;
using System.Collections.Generic;

public class Grid : MonoBehaviour {

    public Node[ ,] grid;
    public Vector2 gridSize;
    public float nodeRadius;
    private float nodeDiameter;
    public LayerMask whatLayer;

    public int gridCntX, gridCntY;
    public Transform player;

    public List<Node> path = new List<Node>( );

    void Start( ) {
        nodeDiameter = nodeRadius * 2;
        gridCntX = Mathf.RoundToInt(gridSize.x / nodeDiameter);
        gridCntY = Mathf.RoundToInt(gridSize.y / nodeDiameter);
        grid = new Node[gridCntX, gridCntY];
        CreateGrid( );
    }

    private void CreateGrid( ) {
        Vector3 startPoint = transform.position - gridSize.x / 2 * Vector3.right - Vector3.forward * gridSize.y / 2;
        for (int i = 0; i < gridCntX; i++) {
            for (int j = 0; j < gridCntY; j++) {
                Vector3 worldPoint = startPoint + Vector3.right * (i * nodeDiameter + nodeRadius) +
                    Vector3.forward * (j * nodeDiameter + nodeRadius);
                bool walkable = !Physics.CheckSphere(worldPoint, nodeRadius, whatLayer);
                grid[i, j] = new Node(walkable, worldPoint, i, j);
            }
        }
    }

    public Node GetFromPosition(Vector3 postion) {
        float percentX = (postion.x + gridSize.x / 2) / gridSize.x;
        float percentY = (postion.z + gridSize.y / 2) / gridSize.y;
        percentX = Mathf.Clamp01(percentX);
        percentY = Mathf.Clamp01(percentY);

        int x = Mathf.RoundToInt((gridCntX - 1) * percentX);
        int y = Mathf.RoundToInt((gridCntY - 1) * percentY);

        return grid[x, y];
    }

    void OnDrawGizmos( ) {
        Gizmos.DrawWireCube(transform.position, new Vector3(gridSize.x, 1, gridSize.y));
        if (null == grid) return;
        Node playerNode = this.GetFromPosition(player.position);
        foreach (var node in grid) {
            Gizmos.color = node._Canwalk ? Color.white : Color.red;
            Gizmos.DrawCube(node._worldPos, Vector3.one * (nodeDiameter - 0.1F));
        }
        if (null != path) {
            foreach (var node in path) {
                Gizmos.color = Color.black;
                Gizmos.DrawCube(node._worldPos, Vector3.one * (nodeDiameter - 0.1F));
            }
        }
        if (null != playerNode && playerNode._Canwalk) {
            Gizmos.color = Color.cyan;
            Gizmos.DrawCube(playerNode._worldPos, Vector3.one * (nodeDiameter - 0.1F));
        }
    }

    public List<Node> GetNeibourhood(Node node) {
        List<Node> neibourhood = new List<Node>( );
        //i，j是-1 列表中是1，但是咱们要的是数组，但是数组是从0开始的
        for (int i = -1; i < 1; i++) {
            for (int j = -1; j < 1; j++) {
                if (i == 0 && j == 0) {
                    continue;
                }
                int tempX = node.gridX + i;
                int tempY = node.gridY + j;
                //这是我另一个出错的地方，tempY的判断反了
                if (tempX < gridCntX && tempX > 0 && tempY < gridCntY && tempY > 0) {
                    neibourhood.Add(grid[tempX, tempY]);
                }
            }
        }
        return neibourhood;
    }

}
